<?php

/**
 *  This collection of functions (library) is licensed under GPL2.
 *  See LICENSE or www.gnu.org for more details.
 *
 *  @author Jyry Kuukkanen
 *
 *  $Id: dzlang.php 595 2006-04-03 16:00:47Z jyry $
 */


include_once("sodebug.php");
include_once("soinifiles.php");


/** Class to handle translations for certain form or other purposes.
 *  @var soIniData $__LangIni Translations
 */
class dzLang {

    /* @private */
    var $__LangIni;         /* soIniData object, translations */


    /** Constructor
     *  Initialises class properties
     *  @param string $LangFile Language file to get translations from.
     */
    function dzLang($LangFile = "") {
        $this->__LangIni = NULL;
        if ($LangFile) $this->readIni($LangFile);
    } // Constructor


    /** Reads in translation ini stream
     *  @param string $LangFile Language file to get translations from.
     */
    function readIni($LangFile) {
        soDebug("dzLang->readIni: langfile='".$LangFile."'", SOD_DET);
        if (!$this->__LangIni) {
            $this->__LangIni = new soIni($LangFile);
        } else {
            $this->__LangIni->read($LangFile, 2);
        }; // else if
    } // readIni


    /** Returns translation text for requested tag.
     *  e.g. getTranslation("tagname", "fi", "tip") would return tip text
     *  in Finnish for dzcol "tagname".
     *  @param string $Tag Tag to be translated
     *  @param string $Language Language to translate to
     *  @param string $Type Which type of translation (tip, help, label etc.)
     *  @return string Translated text or tag when no translation available.
     */
    function getTranslation($Tag, $Language, $Type) {
        $Language = strtolower($Language);
        $set = $this->__LangIni->getValue($Tag, $Language, NULL);
        if ($set) {
            /* translations available */
            $result = $set->getItem($Type);
            $this->Error = 0;
        } else { // else if set->size()
            /* If no translations were found for a certain language, then
             * try with language code "default". */
            if ($Language != "default") {
                $result = $this->getTranslation($Tag, "default", $Type);
                if (!$result) {
                    $result = $Tag;
                    $this->Error = 1;
                };
            } else {
                $result = "";
            }; // else if
        };

        return $result;
    } // getTranslation

}; // dzLang


/** Returns translated system message
 *  @param string $LibName System file name (without extension) to get a translated
 *      system message for.
 *  @param string $Tag Message name to be translated.
 *  @param string $Language Language code to get a translation for.
 *  @return string Translated system message.
 */
function dzGetSysMsg($LibName, $Tag, $Language = "") {
    $library = basename(substr($LibName, 0, soLocateStr($LibName, ".", -1)));
    $path = dzGetDataxiPath();
    $ini = new soIni("file://".$path."/".$library.".msg");

    if (!$Language) {
        $Language =  dzGetIniItem(DZIF_SYS, DZID_LANG);
        if (!$Language) $Language = "default";
    }; // if

    $result = $ini->getValue($Tag, $Language, SOINI_NOSUB);
    if (!$result) $result = $ini->getValue($Tag, "default", SOINI_NOSUB);
    if (!$result) $result = $Tag;

    return $result;
}; // dzGetSysMsg


/** Returns a DB table or column name translated.
 *  Database table and column name translations are stored in a "databasename.txt" file in ini
 *  folder. This enables many routines, like invalid form value check, to produce meaningful
 *  messages to user.
 *  Example: dzGetDBTranslation("tables", "foobar", "fi") could return "Huuhaa" - a Finnish
 *  name for a table "foobar".
 *  Example: dzGetDBTranslation("foobar", "col1", "fi") could return "Sarake1" - a Finnish
 *  name for a column "col1" of table "foobar".
 *  @param string $Database Database name to get translations for
 *  @param string $Section Table name or "tables" when translating a table name.
 *  @param string $Name Table or column name.
 *  @param string $Language Language code.
 *  @return string Translated name for table or column - or untranslated name when not found.
 */
function dzGetDBTranslation($Database, $Section, $Name, $Language = "") {
    $path = dzGetIniPath(DZID_INI_FOLDER);
    $ini = new soIni("file://".$path."/".$Database.".txt");

    if (!$Language) {
        $Language =  dzGetIniItem(DZIF_SYS, DZID_LANG);
        if (!$Language) $Language = "default";
    }; // if

    if ($ini) $result = $ini->getValue($Section, $Name, $Language);
    if (!$result) $result = $ini->getValue($Section, $Name, "default");
    if (!$result) $result = $Name;

    return $result;
}; // dzGetDBTranslation

?>
